package com.example.json;

import com.example.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ws
 * Date: 2024-03-02
 * Time: 19:53
 */

//@RestController == @ResponseBody + @Controller，当有了 @ResponseBody 这个注解，就直接返回数据，不会再走视图解析器（ViewResolver）
@Controller
@RequestMapping("/json")
public class JsonController {


    @ResponseBody // 返回值会自动转成 json
    @GetMapping("/data")
    public User data() {
        User user = new User("dog", 1);
        // user 会被 handlerAdapter 转成 json
        return user;
    }


    @ResponseBody
    @GetMapping("/data2")
    public List<User> data2() {
        ArrayList<User> users = new ArrayList<>();
        users.add(new User("cat", 2));
        return users;
    }
}
